package cn.hospital.controller;
import cn.hospital.entity.Department;
import cn.hospital.entity.Grouptable;
import cn.hospital.entity.MeaningUser;
import cn.hospital.entity.UserDepartment;
import cn.hospital.service.DropDownBoxQueryService;
import cn.hospital.service.LoginRegisteredService;
import cn.hospital.service.MeaningUserService;
import cn.hospital.service.UserDepartmentServie;
import cn.hospital.util.PageBean;
import cn.hospital.util.PageInfo;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.HashMap;

import java.util.List;
import java.util.Map;


@Controller

public class SystemController {

    @Resource
    private MeaningUserService meaningUserService;
    @Resource
    private  LoginRegisteredService loginRegisteredService;
    @Resource
    private DropDownBoxQueryService dropDownBoxQueryService;
    @Resource
    private UserDepartmentServie userDepartmentServie;


    /**
     * 分页查询
     * @param meaningUser
     * @param model
     * @return
     */
    @RequestMapping("/selectuser")
    public String selectuser(MeaningUser meaningUser, Model model,Integer currentPage){
        System.out.println(meaningUser);
        PageBean page = new PageBean();
        if(null != currentPage) {
            page.setCurrentPage(currentPage);
        }else {
            page.setCurrentPage(1);
        }
        meaningUserService.selectuser(meaningUser,page);
        model.addAttribute("page",page);
        model.addAttribute("meaningUser",meaningUser);
        return "page/system/user";
    }

    /**
     * 添加方法
     * @param
     * @return
     */
    @RequestMapping("/addMeaningUser")
    public String addMeaningUser(MeaningUser meaningUser){
        int  row=  loginRegisteredService.addByMeaningUser(meaningUser);
        return "redirect:/selectuser";
    }
    /**
     * 删除user
     */
    @ResponseBody
    @RequestMapping("/deladdMeaningUser")
    public Map<String,Object> delMeaningUser(Integer id){
        int  row=meaningUserService.delMeaningUser(id);
        Map<String,Object> map=new HashMap<>();
        System.out.println(row);
        map.put("row",row);
        return map;
    }

    /**
     * 修改
     * @param
     * @return
     */
    @RequestMapping("/upMeaningUser")
    @ResponseBody
    public Map<String,Object> upMeaningUser(MeaningUser meaningUser){
        int  row=meaningUserService.upMeaningUser(meaningUser);
        Map<String,Object> map=new HashMap<>();
        map.put("row",row);
        return map;
    }

    /**
     * 根据医院id获取科室列表
     * @return
     */
    @RequestMapping("/getkslist")
    @ResponseBody
    public Map<String,Object>getkslist(HttpSession session){
        Map<String,Object> map=new HashMap<>();
        List<Department> departmentlist=dropDownBoxQueryService.findByhospitalId(session);
        map.put("departmentlist",departmentlist);
        return map;
    }

    /**
     * 通过id回显修改内容
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectuserbyid")
    public Map<String,Object> selectuserbyid(Integer id){
        MeaningUser User= meaningUserService.selectuserbyid(id);
        Map<String,Object> map=new HashMap<>();
        map.put("User",User);
        return map;
    }
//=============================================================================================================
    /**
     * 通过科室id查询科室人员
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getuserlist")
    public Map<String,Object> selectuserlist(Integer id){
        List<MeaningUser>meaningUserlist=meaningUserService.selectuserlist(id);
        Map<String,Object> map=new HashMap<>();
        map.put("meaningUserlist",meaningUserlist);
        return map;
    }

    /**
     * 将科室管理信息添加到科室管理表
     */
    @ResponseBody
    @RequestMapping("/adddepement")
    public Map<String,Object> adddepement(UserDepartment userDepartment){
        int row =userDepartmentServie.adddepement(userDepartment);
        Map<String,Object> map=new HashMap<>();
        map.put("row",row);
        return map ;//"redirect:/selectuser"
    }

    /**
     * 查询科室管理表，所有信息
     * @return
     */
    @RequestMapping("/selectUserDepartment")
    public String selectUserDepartment(Model model){
        List<UserDepartment> userDepartmentList=userDepartmentServie.selectUserDepartment();
        model.addAttribute("userDepartmentList",userDepartmentList);
        return "page/system/permission";
    }
    /**
     * 根据条件查询科室管理信息
     */
    @ResponseBody
    @RequestMapping("/selectdepement")
    public Map<String,Object>selectdepement(Integer id){
        UserDepartment userDepartment=userDepartmentServie.selectdepementbyid(id);
        Map<String,Object> map=new HashMap<>();
        map.put("userDepartment",userDepartment);
        return map;
    }

    /**
     * 删除科室管理员
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deldepementid")
    public Map<String,Object> deldepementbyid(Integer id){
        Map<String,Object> map=new HashMap<>();
        int row=userDepartmentServie.deldepementbyid(id);
        map.put("row",row);
        return map;
    }

    /**
     * 根据科室id查询科室是否添加过管理员
     */
    @ResponseBody
    @RequestMapping("/selectdepementbyksid")
    public boolean selectdepementbyksid(Integer ksid){
        boolean row=userDepartmentServie.selectdepementbyksid(ksid);

        return row;
    }

    /**
     * 保存修改信息
     * @param userDepartment
     * @return
     */
    @RequestMapping("/upUserDepartment")
    @ResponseBody
    public Map<String,Object> upUserDepartment(UserDepartment userDepartment){
        Map<String,Object> map=new HashMap<>();
        int row=userDepartmentServie.upUserDepartment(userDepartment);
        map.put("row",row);
        return map;
    }

    /**
     * 编辑查询回显
     * @param id
     * @return
     */
    @RequestMapping("/selectdepementbyid")
    @ResponseBody
    public Map<String,Object> selectdepementbyid(Integer id) {
        Map<String, Object> map = new HashMap<>();
        UserDepartment UserDepartment = userDepartmentServie.selectdepementbyid(id);
        map.put("UserDepartment", UserDepartment);
        return map;
    }

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/tzlogin")
    public String tzlogin(HttpSession session){
        session.invalidate();
        return"redirect:/page/LoginRegister/login";
    }
}
